using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using AutoBase.Shared;
using AutoBase.BusinessObjects;

public partial class SearchBoxLong : System.Web.UI.UserControl
{
    #region PROPERTIES

    /// <summary>
    /// Search By branch id
    /// </summary>
    //[Bindable(true)]
    //[Category("Data")]
    //[DefaultValue("")]
    //[Localizable(true)]
    public int BranchId
    {
        get
        {
            object o = this.ViewState["_BranchId"];
            if (o == null)
            {
#if (DEBUG)
                    return 2277;
#endif
                return -1;
            }

            else
            {
                return (int)o;
            }
        }

        set
        {
            this.ViewState["_BranchId"] = (object)value;
        }
    }

    //[Bindable(true)]
    //[Category("Data")]
    //[DefaultValue("")]
    //[Localizable(true)]
    public string BranchIdLocationList
    {
        get
        {
            object o = this.ViewState["_BranchIdLocationList"];
            if (o == null)
            {
#if (DEBUG)
                    return "638|2264|2266";
#endif
                return string.Empty;
            }
            else
                return (string)o;
        }
        set
        {
            this.ViewState["_BranchIdLocationList"] = (object)value;
        }
    }

    //[Bindable(true)]
    //[Category("Data")]
    //[DefaultValue("")]
    //[Localizable(true)]
    public bool IsVisibleSearchButon
    {
        get
        {
            object o = this.ViewState["_IsVisibleSearchButon"];
            if (o == null)
                return true;
            else
                return (bool)o;
        }
        set
        {
            this.ViewState["_IsVisibleSearchButon"] = (object)value;
        }
    }

    public int SelectedManufacturerId
    {
        get
        {
            object o = this.ViewState["_SelectedManufacturerId"];
            if (o == null)
            {
                return -1;
            }
            else
            {
                return (int)o;
            }
        }

        set
        {
            this.ViewState["_SelectedManufacturerId"] = (object)value;
        }
    }

    public int SelectedModelId
    {
        get
        {
            object o = this.ViewState["_SelectedModelId"];
            if (o == null)
            {
                return -1;
            }
            else
            {
                return (int)o;
            }
        }
        set
        {
            this.ViewState["_SelectedModelId"] = (object)value;
        }
    }

    public int SelectedYearTo
    {
        get
        {
            object o = this.ViewState["_SelectedYearTo"];
            if (o == null)
            {
                return -1;
            }
            else
            {
                return (int)o;
            }
        }
        set
        {
            this.ViewState["_SelectedYearTo"] = (object)value;
        }
    }

    public int SelectedYearFrom
    {
        get
        {
            object o = this.ViewState["_SelectedYearFrom"];
            if (o == null)
            {
                return -1;
            }
            else
            {
                return (int)o;
            }
        }
        set
        {
            this.ViewState["_SelectedYearFrom"] = (object)value;
        }
    }

    public decimal SelectedPriceFrom
    {
        get
        {
            object o = this.ViewState["_SelectedPriceFrom"];
            if (o == null)
            {
                return -1;
            }
            else
            {
                return (decimal)o;
            }
        }
        set
        {
            this.ViewState["_SelectedPriceFrom"] = (object)value;
        }
    }

    public decimal SelectedPriceTo
    {
        get
        {
            object o = this.ViewState["_SelectedPriceTo"];
            if (o == null)
            {
                return -1;
            }
            else
            {
                return (decimal)o;
            }
        }
        set
        {
            this.ViewState["_SelectedPriceTo"] = (object)value;
        }
    }

    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadManufacturer();
        }
        

    }
    protected void uicManufacturer_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        LoadModels();

    }

    private void LoadManufacturer()
    {
        try
        {
            VehicleSearchManger.WriteLine(this.ID.ToString(), "Loading Manufacturer... - BranchLocationList:" + BranchIdLocationList.ToString());
            List<string> BranchLocationList = new List<string>();

            VehicleSearchManger.WriteLine(this.ID.ToString(), "BranchIdLocationList:" + BranchIdLocationList.ToString());
            //BranchLocationList.AddRange(new string[]{"638","2264","2266"});
            BranchLocationList.AddRange(this.BranchIdLocationList.Split('|'));
            VehicleSearchManger.WriteLine(this.ID.ToString(), "BranchLocationList:" + BranchIdLocationList.ToString());

            VehicleSearchManger.WriteLine(this.ID.ToString(), "BranchLocationListCount:" + BranchLocationList.Count.ToString());
            if (BranchLocationList == null || BranchLocationList.Count == 0)
                return;

            uicManufacturer.DataSource = VehicleSearchManger.LoadManufacturer(BranchLocationList, 2277);
            uicManufacturer.DataValueField = ViewDistinctManufacturerModelByBranchIdMetadata.ColumnNames.ManufacturerId;
            uicManufacturer.DataTextField = ViewDistinctManufacturerModelByBranchIdMetadata.ColumnNames.ManufacturerName;

            uicManufacturer.DataBind();

            VehicleSearchManger.WriteLine(this.ID.ToString(), "Binded to uicManufacturer...Successful!");

            uicManufacturer.Items.Insert(0, new ListItem("All Makes", "0"));

            uicModel.Items.Insert(0, new ListItem("All Models", "0"));
            VehicleSearchManger.WriteLine(this.ID.ToString(), "Loading Manufacturer...Successful!");
        }
        catch (Exception ex)
        { 
            VehicleSearchManger.WriteLine(this.ID.ToString(), "ERROR Loading Manufacturer..." + ex.Message + "; " + ex.StackTrace); 
        }
    }

    private void LoadModels()
    {
        VehicleSearchManger.WriteLine(this.ID.ToString(), "Loading Models...; BranchIdLocationList:" + BranchIdLocationList);
        if (Convert.ToInt64(Request.QueryString["id"]) > 0)
        {
            Response.Redirect(System.IO.Path.GetFileName(Request.CurrentExecutionFilePath));
        }

        List<string> BranchLocationList = new List<string>();

        //BranchLocationList.AddRange(new string[]{"638","2264","2266"});
        BranchLocationList.AddRange(this.BranchIdLocationList.Split('|'));

        if (BranchLocationList.Count > 0)
        {
            uicModel.DataSource = VehicleSearchManger.LoadModels(
                                                  Convert.ToInt32(uicManufacturer.SelectedValue),
                                                  BranchLocationList,
                                                  null);
            uicModel.DataValueField = ViewDistinctManufacturerModelByBranchIdMetadata.ColumnNames.ModelID;
            uicModel.DataTextField = ViewDistinctManufacturerModelByBranchIdMetadata.ColumnNames.ModelName;

            uicModel.DataBind();

            uicModel.Items.Insert(0, new ListItem("All Models", "0"));
        }
        VehicleSearchManger.WriteLine(this.ID.ToString(), "Loading Models...Successful!");
    }


    protected void uicYearFrom_TextChanged(object sender, EventArgs e)
    {


    }
    protected void uicSearch_Click(object sender, EventArgs e)
    {
        SelectedManufacturerId = (uicManufacturer.SelectedIndex > 0 ? Convert.ToInt32(uicManufacturer.SelectedValue) : -1);
        SelectedModelId = (uicModel.SelectedIndex > 0 ? Convert.ToInt32(uicModel.SelectedValue) : -1);
        SelectedPriceFrom = (uicPriceFrom.Text.Length > 0 ? Convert.ToDecimal(uicPriceFrom.Text) : -1);
        SelectedPriceTo = (uicPriceTo.Text.Length > 0 ? Convert.ToDecimal(uicPriceTo.Text) : -1);
        SelectedYearFrom = (uicYearFrom.Text.Length > 0 ? Convert.ToInt32(uicYearFrom.Text) : -1);
        SelectedYearTo = (uicYearTo.Text.Length > 0 ? Convert.ToInt32(uicYearTo.Text) : -1);

        SearchButtonClicked(sender, e);
    }

    public event EventHandler SearchButtonClicked;

}
